This dashboard displays information of inspection for restaurants in Manhattan, New York.

Column

Chart A

Column

Chart B

Chart C

---
title: "Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(plotly)
library(httr)
library(jsonlite)
```

This dashboard displays information of inspection for restaurants in Manhattan, New York.

```{r}
get_all_inspections = function(url) {
  
  all_inspections = vector("list", length = 0)
  
  loop_index = 1
  chunk_size = 50000
  DO_NEXT = TRUE
  
  while (DO_NEXT) {
    message("Getting data, page ", loop_index)
    
    all_inspections[[loop_index]] = 
      GET(url,
          query = list(`$order` = "zipcode",
                       `$limit` = chunk_size,
                       `$offset` = as.integer((loop_index - 1) * chunk_size)
                       )
          ) %>%
      content("text") %>%
      fromJSON() %>%
      as_tibble()
    
    DO_NEXT = dim(all_inspections[[loop_index]])[1] == chunk_size
    loop_index = loop_index + 1
  }
  
  all_inspections
  
}

url = "https://data.cityofnewyork.us/resource/43nn-pn8j.json"

nyc_inspections = 
  get_all_inspections(url) %>%
  bind_rows() 
```



```{r}
rest_manhattan = 
  nyc_inspections %>%
  filter(boro == "Manhattan") %>% 
  drop_na() %>% 
  mutate(
    long = as.numeric(latitude),
    lat = as.numeric(longitude))#Latitude and longitude data seem to be the other way around.
```


Column {data-width=550}
-----------------------------------------------------------------------

### Chart A

```{r}
rest_manhattan %>% 
  filter(grade %in% c("A","B","C")) %>% 
  mutate(
    text_label = str_c("Name:",dba,"\nGrade:",grade)
  ) %>% 
  plot_ly(
    x = ~lat, y = ~long,type = "scatter",mode ="markers",
    color =~grade,text = ~text_label, alpha = 0.5
  )
```

Column {data-width=550}
-----------------------------------------------------------------------

### Chart B

```{r}
rest_manhattan_popular = rest_manhattan %>% 
  count(cuisine_description) %>% 
  mutate(cuisine_description = fct_reorder(cuisine_description, n ,.desc = TRUE))

popular_30 = levels(pull(rest_manhattan_popular,cuisine_description))[1:30]


rest_manhattan_popular %>% 
  filter(cuisine_description %in%  popular_30) %>% 
  plot_ly(x = ~cuisine_description, y = ~n, color = ~cuisine_description, type = "bar", colors = "viridis")
```

### Chart C

```{r}
rest_manhattan %>% 
  mutate(score = as.numeric(score)) %>% 
  filter(cuisine_description %in%  popular_30) %>% 
  plot_ly(y = ~score, color = ~cuisine_description, type = "box",
          colors = "viridis")
```